package a_o2odata_deal.utils.zhibo_zf

import com.alibaba.fastjson.JSON
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/1/15 12:28
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object modify_qiye {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")


    val tm_all_data = sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/tmall/all_userid/*/*")
        .selectExpr("userId").dropDuplicates()
    val tb_all_data = sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/taobao/all_userid/*/*")
      .selectExpr("userId").dropDuplicates()
    val all_data = tm_all_data.union(tb_all_data).dropDuplicates()
    all_data.createOrReplaceTempView("all_data")

    import org.elasticsearch.spark._
    for (months <- 1 to 12){
      /*val all_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/zhibo_zf/zhubo/${months}/")
      all_data.createOrReplaceTempView("all_data")*/
      //all_data.printSchema()

      val value: RDD[String] = sc.esJsonRDD(s"2019_tmall_anchor/tmall_anchor_2019_${months}").values
      sqlContext.read.json(value).createOrReplaceTempView("tm_data")

      val value1: RDD[String] = sc.esJsonRDD(s"2019_taobao_anchor/taobao_anchor_2019_${months}").values
      sqlContext.read.json(value1).createOrReplaceTempView("tb_data")

      val result_tm = sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case when t2.userId is null then 'false' else 'true' end as is_selfLives
           |from tm_data t1
           |left join
           |all_data t2
           |on t1.anchorId=t2.userId
         """.stripMargin).drop("is_selfLive")
        .withColumnRenamed("is_selfLives","is_selfLive")

      val result_tb = sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case when t2.userId is null then 'false' else 'true' end as is_selfLives
           |from tb_data t1
           |left join
           |all_data t2
           |on t1.anchorId=t2.userId
         """.stripMargin).drop("is_selfLive")
        .withColumnRenamed("is_selfLives","is_selfLive")

      /*println("天猫总数:"+result_tm.count())
      println("天猫店铺主播数量:"+result_tm.where("is_selfLive='true'").count())
      println("天猫达人主播数量:"+result_tm.where("is_selfLive='false'").count())

      println("淘宝总数:"+result_tb.count())
      println("淘宝店铺主播数量:"+result_tb.where("is_selfLive='true'").count())
      println("淘宝达人主播数量:"+result_tb.where("is_selfLive='false'").count())*/

      result_tm.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2019_tmall_anchor/tmall_anchor_2019_${months}", Map("es.mapping.id" -> "anchorId"))

      result_tb.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2019_taobao_anchor/taobao_anchor_2019_${months}", Map("es.mapping.id" -> "anchorId"))


    }

    //val tm_all_data = sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/tmall/all_userid/*/*").dropDuplicates()
    //val tb_all_data = sqlContext.read.json("s3a://o2o-dataproces-group/panzonghao/taobao/all_userid/*/*").dropDuplicates()
    //tm_all_data.createOrReplaceTempView("tm_all_data")
    //tb_all_data.createOrReplaceTempView("tb_all_data")

    for (months <- 1 to 12){
      /*val value: RDD[String] = sc.esJsonRDD(s"2019_tmall_anchor/tmall_anchor_2019_${months}").values
      sqlContext.read.json(value).createOrReplaceTempView("tm_source_data")

      val value1: RDD[String] = sc.esJsonRDD(s"2019_taobao_anchor/taobao_anchor_2019_${months}").values
      sqlContext.read.json(value1).createOrReplaceTempView("tb_source_data")

      //增加标签 is_selfShopAnchor 用于区分天猫淘宝店铺主播
      val result_tm = sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case when t2.userId is null then 'false' else 'true' end as is_selfShopAnchor
           |from tm_source_data t1
           |left join
           |tm_all_data t2
           |on t1.anchorId=t2.userId
         """.stripMargin)

      val result_tb = sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case when t2.userId is null then 'false' else 'true' end as is_selfShopAnchor
           |from tb_source_data t1
           |left join
           |tb_all_data t2
           |on t1.anchorId=t2.userId
         """.stripMargin)*/


      /*result_tm.where("anchorId='1879992590'").show(false)
      result_tm.where("anchorId='385911725'").show(false)
      result_tb.where("anchorId='1879992590'").show(false)
      result_tb.where("anchorId='385911725'").show(false)
      println("天猫总数:"+result_tm.count())
      println("天猫店铺主播数量:"+result_tm.where("is_selfShopAnchor='true'").count())
      println("天猫达人主播数量:"+result_tm.where("is_selfShopAnchor='false'").count())

      println("淘宝总数:"+result_tb.count())
      println("淘宝店铺主播数量:"+result_tb.where("is_selfShopAnchor='true'").count())
      println("淘宝达人主播数量:"+result_tb.where("is_selfShopAnchor='false'").count())*/


      /*result_tm.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2019_tmall_anchor/tmall_anchor_2019_${months}", Map("es.mapping.id" -> "anchorId"))

      result_tb.toJSON.rdd.map(line =>{
        JSON.parseObject(line)
      }).saveToEs(s"2019_taobao_anchor/taobao_anchor_2019_${months}", Map("es.mapping.id" -> "anchorId"))*/

    }

  }

}
